package zhengdaoyang.settings.web.controller;

import zhengdaoyang.settings.domain.Cart;
import zhengdaoyang.settings.service.CartService;
import zhengdaoyang.settings.service.impl.CartServiceImpl;
import zhengdaoyang.utils.PrintJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CartController extends HttpServlet {
    private final CartService cartService = new CartServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入购物车控制层");

        String path = request.getServletPath();
        if ("/settings/cart/findallcart".equals(path)) {
            findallcart(request, response);
        } else if ("/settings/cart/updateCart".equals(path)) {
            updateCart(request, response);
        }else  if("/settings/cart/deleteCartGoods".equals(path)){
            deleteCartGoods(request,response);
        }else if("/settings/cart/deleteAllCartGoods".equals(path)){
            deleteAllCartGoods(request,response);
        }else if ("/settings/cart/insertorders".equals(path)){
            insertorders(request,response);
        }
    }

    /*向order表插入信息*/
    private void insertorders(HttpServletRequest request, HttpServletResponse response) {
        String uid = request.getParameter("uid");
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");
        String username = request.getParameter("username");
        String omoney = request.getParameter("omoney");

        try {
            cartService.insertorders(uid,address,phone,username,omoney);
            System.out.println("回到购物车控制层");
            PrintJson.printJsonFlag(response, true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*删除购物车所有物品*/
    private void deleteAllCartGoods(HttpServletRequest request, HttpServletResponse response) {
       try {
           cartService.deleteAllCartGoods();
           System.out.println("回到购物车控制层");
           PrintJson.printJsonFlag(response, true);
       }catch (Exception e){
           e.printStackTrace();
       }
    }

    /*获取购物车所有的数据*/
    private void findallcart(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始获取");
        try {
            List<Cart> cartList = cartService.findallcart();
            System.out.println("成功返回购物车控制层");
            System.out.println(cartList + "===购物车控制层");
            PrintJson.printJsonObj(response, cartList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*更新购物车商品数量*/
    private void updateCart(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始获取");
        try {
            String nu = request.getParameter("num");
            String name = request.getParameter("name");
            int num = Integer.parseInt(nu);

            int kk = cartService.updateCart(num, name);
            System.out.println("回到购物车控制层");
            PrintJson.printJsonFlag(response, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void deleteCartGoods(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        try{
          int kk =   cartService.deleteCartGoods(name);
            System.out.println("回到购物车控制层");
            PrintJson.printJsonFlag(response, true);
        }catch (Exception e){
            e.printStackTrace();;
        }
    }
}
